<?php
/**
 * 作品管理
 * @copyright (c) 17wanya All Rights Reserved
 */

class Productions_Model {

	private $db;

	function __construct() {
		$this->db = Database::getInstance();
	}

	/**
	 * 得到作品总数
	 *
	 * @param string $open_id
	 */
	function getProductionSumByOpenid($openid) {
		$sql = "SELECT count(*) as total FROM " . DB_PREFIX . "productions WHERE openid = '$openid'";
		$res = $this->db->once_fetch_array($sql);
		return $res['total'];;
	}


	/**
	 * 得到被推荐的作品
	 */
	function getRecommendProductions() {
		$sql = "select wanya_productions.id,wanya_productions.production_url,wanya_productions.production_explain,wanya_productions.production_type,wanya_productions.vote_num,wanya_view_user_info.nickname,wanya_view_user_info.head_img_url from wanya_productions"
				." left join wanya_view_user_info"
				." on wanya_productions.openid = wanya_view_user_info.openid"
				." where recommend_flg = 1"
				." ORDER BY wanya_productions.vote_num desc";
		$res = $this->db->query($sql);
		$recommendProductions = array();
		while ($row = $this->db->fetch_array($res)) {
			$recommendProductions[] = $row;
		}
		return $recommendProductions;
	}


	/**
	 * 得到作品的详情
	 */
	function getProductionDetails($productionId) {
		$sql = "select wanya_productions.activity_id,wanya_productions.production_url,wanya_productions.production_type,wanya_productions.production_explain,wanya_view_user_info.head_img_url,wanya_view_user_info.nickname "
			." from wanya_productions "
			." LEFT JOIN wanya_view_user_info"
			." on wanya_view_user_info.openid = wanya_productions.openid"
			." where wanya_productions.id= $productionId";
		$res = $this->db->once_fetch_array($sql);
		return $res;
	}


	/**
	 * 得到赞作品的人
	 */
	function getProductionVoteUser($productionId) {
		$sql = "select nickname,wanya_production_vote.head_img_url,create_time"
			." from wanya_production_vote"
			." left join wanya_view_user_info"
			." on wanya_production_vote.openid = wanya_view_user_info.openid"
			." where wanya_production_vote.production_id = $productionId"
			." ORDER BY wanya_production_vote.create_time desc";
		$res = $this->db->query($sql);
		$productionVoteUsers = array();
		while ($row = $this->db->fetch_array($res)) {
			$productionVoteUsers[] = $row;
		}
		return $productionVoteUsers;
	}

	/**
	 * 得到作品的评论
	 */
	function getProductionReply($productionId) {
		$sql = "select nickname,wanya_production_comment.head_img_url,content,create_time"
				." from wanya_production_comment"
				." left join wanya_view_user_info"
				." on wanya_production_comment.openid = wanya_view_user_info.openid"
				." where wanya_production_comment.production_id = $productionId"
				." ORDER BY wanya_production_comment.create_time desc";
		$res = $this->db->query($sql);
		$productionReply = array();
		while ($row = $this->db->fetch_array($res)) {
			$productionReply[] = $row;
		}
		return $productionReply;
	}

	/**
	 * 赞作品
	 */
	function voteProduction($voteProductionData) {
		$time=date("Y-m-d H:i:s");
		$openid=$voteProductionData["openid"];
		$head_img_url=$voteProductionData["head_img_url"];
		$production_id=$voteProductionData["production_id"];

		$sql="insert into ".DB_PREFIX."production_vote (openid,head_img_url,production_id,create_time,modify_time) values('$openid','$head_img_url','$production_id','$time','$time')";
		$this->db->query($sql);

		$this->db->query("update ".DB_PREFIX."productions set vote_num=vote_num+1 where id=$production_id");
	}

	/**
	 * 评论作品
	 */
	function replyProduction($replyProductionData) {
		$time=date("Y-m-d H:i:s");
		$openid=$replyProductionData["openid"];
		$head_img_url=$replyProductionData["head_img_url"];
		$production_id=$replyProductionData["production_id"];
		$content=$replyProductionData["content"];

		$sql="insert into ".DB_PREFIX."production_reply (openid,head_img_url,production_id,content,create_time,modify_time) values('$openid','$head_img_url','$production_id','$content','$time','$time')";
		$this->db->query($sql);
		$this->db->query("update ".DB_PREFIX."productions set reply_num=reply_num+1 where id=$production_id");
	}

	/**
	 * 检索当前活动的文件数和赞数
	 */
	function getProductionCount($activity_id,$openid){
		$sql = "select count(1) as productionSum,sum(vote_num) as voteSum from ".DB_PREFIX
			."productions where activity_id=$activity_id and activity_id = $openid";
		$res= $this->db->once_fetch_array($sql);
		return $res;
	}

	/**
	 * 新增活动作品
	 * @param $production
	 * @return int
	 */
	function  addProduction($production){
		$time=date("Y-m-d H:i:s");
		$openid = $production['openid'];
		$activity_id = $production['activity_id'];
		$sub_activity_id = $production['sub_activity_id'];
		//默认为0 图片
		$production_type = 0;
		$production_url = $production['production_url'];
		$production_explain = $production['production_explain'];

		$sql = " insert into ".DB_PREFIX."productions (openid,activity_id,sub_activity_id,production_type,production_explain,create_time,modify_time) values ($openid,$activity_id,$sub_activity_id,$production_url,$production_type,$production_explain,$time,$time)";
		$this->db->query($sql);
		return mysql_insert_id();
	}
}
